<template>
  <div class="home">
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>关键词：</label>
            <el-input type="text" placeholder="请输入关键词" v-model="keyword" clearable></el-input>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span class="operate_btn search_btn" @click="openPopup('')">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="80"
            prop="id"
            label="ID">
          </el-table-column>
          <el-table-column
            width="160"
            sortable
            prop="modifyTime"
            label="修改时间">
            <template slot-scope="scope">
              <p style="text-align: left">{{formatTime(scope.row.modifyTime)}}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="类型">
          </el-table-column>
          <el-table-column
            label="图标">
            <template slot-scope="scope">
              <img :src="scope.row.photoUrl" alt="tzs" class="icon" v-if="scope.row.photoUrl">
              <p v-else>——</p>
            </template>
          </el-table-column>
          <el-table-column
            width="120"
            prop="type"
            label="是否显示">
            <template slot-scope="scope">
              <div :style="'color:'+(scope.row.isDisplay===1?'#67c430':'#e60000')">
                {{scope.row.isDisplay===1?'是':'否'}}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            width="120"
            prop="sort"
            label="排序">
          </el-table-column>
          <el-table-column
            prop="memo"
            label="标注">
            <template slot-scope="scope">
              <p>{{scope.row.memo?scope.row.memo:'——'}}</p>
            </template>
          </el-table-column>
          <el-table-column
            width="120"
            label="状态" v-if="!(btn.freeze||btn.unfreeze)">
            <template slot-scope="scope">
              <div :style="'color:'+(scope.row.enable===1?'#67c430':'#e60000')">
                {{scope.row.enable===1?'可用':'禁用'}}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="状态"
            prop="2"
            sortable="custom"
            width="100" v-if="btn.freeze||btn.unfreeze">
            <template slot-scope="scope">
              <div>
                <el-switch
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="deleteItem(scope.row,scope.$index)"
                  v-model="scope.row.enable">
                </el-switch>
                <span class="normal" v-if="scope.row.enable===1">启用</span>
                <span class="unusual" v-else>禁用</span>
              </div>
            </template>
          </el-table-column>


          <el-table-column
            label="操作"
            fixed="right"
            v-if="btn&&(btn.update)"
            width="150">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="openPopup(scope.row.id)" v-if="btn.update">编辑</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{totals}}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <edit v-if="isPop" :typeId="typeId" @listenToChild="listenToEdit"></edit>
  </div>
</template>

<script>

import edit from "./edit";
import commons from "@/utils/common";
import { listData, updateEnable } from "@/api/config/productType";
import {getBtn} from "@/utils/btnAuth";

export default {
    components: {
      edit
    },
    data() {
      return {
        keyword:'',
        currentPage: 1,
        pageSize: 15,
        totals: 0,
        loading: false,
        tableInfo:[],
        isPop: false,
        typeId: '',
        firstMenu: '',
        btn:{},
      }
    },
    mounted() {
      this.btn = getBtn();
      if (this.btn.query){
        this.getTableInfo(1);
      }
    },
    methods: {
      // 获取列表信息
      getTableInfo(page) {
        let that = this;
        that.loading = true;
        that.currentPage = page;
        let queryParams = {
          limit:that.pageSize,
          page:page,
          name:that.keyword,
        }
        listData(queryParams).then(res => {
          that.tableInfo = res?res.list:[];
          that.totals = res?res.count:0;
          that.loading = false;
        }).catch((e) => {
          that.loading = false;
        });
      },

      // 重置
      resetTable() {
        this.pageSize = 15;
        this.keyword = '';
        this.getTableInfo(1)
      },

      // 删除
      deleteItem(obj,index) {
        let that = this;
        that.$confirm('确定'+(obj.enable===0?'禁用':'启用')+obj.name+'?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let param = {id:obj.id,enable:obj.enable}
          return updateEnable(param);
        }).then(() => {
          that.getTableInfo(that.currentPage);
          that.$message.success({
            message:'修改成功！',
            duration:1500
          });
        }).catch(() => {
          //重置
          that.tableInfo[index].enable = obj.enable == 0 ? 1:0;
        });
      },

      // 打开弹窗
      openPopup(id) {
        this.isPop = true;
        this.typeId = id;
      },

      listenToEdit(e){
        this.isPop = false;
        if (e.refresh){
          this.getTableInfo(this.currentPage)
        }
      },

      formatTime(time){
        return commons.formatTime(time)
      },

      sizeChange(val){
        this.pageSize =val;
        this.getTableInfo(1)
      },

      // 设置table标题居中
      tableHeader({row, column, rowIndex, columnIndex}) {
        return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
      },
    }
  }
</script>

<style scoped>

</style>
